//
//  HomeVc.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/5.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "MainViewController.h"
#import "MyViewController.h"
#import "SingletonData.h"
#import "CustomerInfoViewController.h"
#import "HomeNewSiteCell.h"
#import "HourseDataManage.h"
#import "config.h"
#import "NodataCell.h"
#import "UINavigationItem+Fixios7.h"
#import "HomeNewSiteCellModel.h"
#import "HourseDetailVc.h"
#import "HomeVcTypeTitleView.h"
#import "MJRefresh.h"
#import "JSONKit.h"
#import "UserModal.h"
#import "MBProgressHUD+MJ.h"
#import "MiddleManMyVC.h"

#import "BrokerDM.h"
#import "DealerMineVc.h"
#import "HomeNewSiteCell2.h"
#import "HomeNewSiteCell3.h"
#import "SearchHomeVC.h"
#import "AppDelegate.h"




@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (nonatomic,strong) HourseDataManage *dm;
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)NodataCell *nodataCell;
@property (nonatomic,strong)NSMutableDictionary *hightDic;
@property (nonatomic,strong)NSArray *advertiseArr;//放广告图片的数据






@end

@implementation MainViewController
@synthesize dm,dataArray,nodataCell,hightDic;


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self initView];
    
    [self refreshDataWithCache:true];
  
   
}

-(void)initView{
    [super initView];
    
   
    [self.tableView setBackgroundColor:[UIColor whiteColor]];
    hightDic = [[NSMutableDictionary alloc]init];
    
    dm = [[HourseDataManage alloc]init];
    
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
     [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    
    {
        
        HomeVcTypeTitleView *title = [[HomeVcTypeTitleView alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
        [title.rightBtn setTag:22];
        [title.rightBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        title.leftBtn.tag = 100;
        [title.leftBtn addTarget:self action:@selector(leftBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];

        if([UserModal shareUserModel].userType != UserTypeCustomer){
          
            title.leftBtn.hidden = YES;
        }else{
            
            title.leftBtn.hidden = NO;
        }
        
    }
    
}


-(void)leftBtnOnclick:(UIButton *)btn{
   
    CustomerInfoViewController *vc = [[CustomerInfoViewController alloc]initWithNibName:@"CustomerInfoViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
    
}


-(void)btnOnclick:(UIButton*)sender{
    
    if (sender.tag==22) {
        
        SearchHomeVC *vc = [SearchHomeVC new];
        vc.hidesBottomBarWhenPushed = YES;
         [self.navigationController pushViewController:vc animated:YES];
        
    }else if(sender.tag==56){
        [self refreshData];
    }
}



#pragma tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray&&dataArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.dataArray.count + 2;
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count==0) {
        if(!nodataCell){
            nodataCell = [[NodataCell alloc] initWithFrame:self.tableView.frame];
            [nodataCell setState:nodataCellStateLoading];
            [[nodataCell refreshBtn]setTag:56];
            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return nodataCell;
    }
    
    if (indexPath.row == 0) {
       
        HomeNewSiteCell3 *cell = [HomeNewSiteCell3 cellWithTableView:tableView];
        cell.advertiseArr = [[NSArray alloc]initWithArray:self.advertiseArr];
        return cell;
    }else if (indexPath.row == 1){
        
        return [HomeNewSiteCell2 cellWithTableView:tableView];
    }
        
        HomeNewSiteCell *cell = [HomeNewSiteCell cellWithTableView:tableView];
        
        [cell setData:[dataArray objectAtIndex:indexPath.row - 2]];
        
        return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (dataArray&&dataArray.count>0) {
            
        if (indexPath.row == 0) {
            
           return  [HomeNewSiteCell3 getHeightWithTableView:tableView withArr:[[NSArray alloc]initWithArray:self.advertiseArr]];
        }else if (indexPath.row == 1){
            return [HomeNewSiteCell2 getHeight];
        }
       return  [HomeNewSiteCell getHight:[dataArray objectAtIndex:indexPath.row -2] withTableView:tableView];
    }
    
    return self.tableView.frame.size.height;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count>0) {
        if (indexPath.row > 1) {
           
            HourseDetailVc *newSiteVC = [[HourseDetailVc alloc] init];
            newSiteVC.hidesBottomBarWhenPushed = true;
            newSiteVC.dataMod = [dataArray objectAtIndex:indexPath.row-2];
            [self.navigationController pushViewController:newSiteVC animated:YES];
        }
        
    }
}



#pragma mark pullTableView delegate
-(void)refreshData{
    [super refreshData];
    
    [self refreshDataWithCache:false];
}



- (void)loadMore{
    [super loadMore];
    
    [self.tableView.footer resetNoMoreData];
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dm loadMoreData];
        
        dispatch_async(dispatch_get_main_queue(), ^{
        [bself.tableView.footer endRefreshing];
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                
            }else if (mod.resltState == ResultFail){//服务器返回的code出错
        
                
            }else{
                
                NSMutableArray *array = [[NSMutableArray alloc]initWithArray:mod.dataMod];
                if (array.count == 0 ||array == nil) {
                    [bself.tableView.footer noticeNoMoreData];
                }
                NSMutableArray *modArr = [NSMutableArray arrayWithArray:bself.dataArray];
                
                [modArr addObjectsFromArray:array];
                bself.dataArray = [[NSArray alloc]initWithArray:modArr];
            }
            
            [bself.tableView reloadData];
            
            
            
            
        });
    });
    
}


-(void)refreshDataWithCache:(BOOL)cache{
    
    [self.tableView.footer resetNoMoreData];
    [nodataCell setState:nodataCellStateLoading];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod =[dm refreshDataWithCache:cache];
        
        ResulteMod *mod2 = [dm getAdvertise];
        bself.advertiseArr = [[NSArray alloc]initWithArray:mod2.dataMod];
        dataArray = mod.dataMod;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                [bself.nodataCell setCodeErrorTip:mod.resultCode];
                
            }else{//返回正常
                
                if(bself.dataArray.count==0){//没数据
                    
                    [bself.nodataCell setState:nodataCellStateNodata];
                    
                }else{//有数据
                    
                    [bself.nodataCell setState:nodataCellStateNorMal];
                }
            }
            
            [bself.tableView reloadData];
            
            [bself.tableView.header endRefreshing];
        });
    });

}

@end
